package com.hontech.bvapp2.device

import com.hontech.bvapp2.utils.ExecException
import com.hontech.bvapp2.utils.toHex
import serial.codec.CodecType
import serial.decodeU8

class Frame (private val buf: ByteArray) {

    val ty = decodeU8(buf, 3)
    val seq = decodeU8(buf, 4)
    val src = decodeU8(buf, 5)
    val dst = decodeU8(buf, 6)

    fun id() = ((src shl 16) or (ty shl 8) or seq)
    fun pollResult() = decodeU8(buf, 7)
    fun cmd() = decodeU8(buf, 7)
    fun ec() = decodeU8(buf, 8)

    fun parse(vararg args: CodecType) {
        var index = 8
        if ((ty == Ty.RES) || (ty == Ty.SIMPLE_RES)) {
            index = 9
            val ec = ec()
            if (ec != 0) {
                throw ExecException(parseError(src, ec))
            }
        } else if (ty == Ty.LOG) {
            index = 7
        }
        for (arg in args) {
            arg.decode(buf, index)
            index += arg.size()
        }
    }

    override fun toString(): String {
        return buf.toHex()
    }
}

private fun parseDrop(ec: Int): String {
    return when (ec) {
        0x11 -> "落杯1超时"
        0x12 -> "落杯2超时"
        0x13 -> "落杯3超时"
        0x14 -> "封膜但是没有杯子"
        0x15 -> "封膜超时"
        0x16 -> "切膜超时"
        0x17 -> "切膜堵转"
        0x18 -> "落杯 但是没有杯子"
        else -> "切膜落杯控制板未知错误:$ec"
    }
}

private fun parseDoor(ec: Int): String {
    return when (ec) {
        0x11 -> "取物门超时"
        0x12 -> "取物门堵转"
        0x13 -> "吸管门关闭失败"
        else -> "门控制板未知错误:$ec"
    }
}

private fun parseHeat(ec: Int): String {
    return when (ec) {
        0x11 -> "温度传感器故障"
        0x12 -> "喷嘴电机堵转"
        0x13 -> "喷嘴电机超时"
        0x14 -> "搅拌盖电机堵转"
        0x15 -> "搅拌盖电机超时"
        0x16 -> "制冰机通信错误"
        0x17 -> "设置参数保存失败"
        0x18 -> "流量计参数无效"
        0x19 -> "注水失败"
        0x1A -> "夹管阀打开超时"
        0x1B -> "夹管阀关闭超时"
        0x1C -> "截流阀移开超时"
        0x1D -> "截流阀复位超时"
        else -> "加热控制板未知错误:$ec"
    }
}

private fun parseCargo1(ec: Int): String {
    return when (ec) {
        0x11 -> "取货电机堵转"
        0x12 -> "取货电机超时"
        0x13 -> "货架电机堵转"
        0x14 -> "货架电机超时"
        0x15 -> "扫码超时"
        0x16 -> "扫码数据错误"
        0x17 -> "气缸往上失败"
        0x18 -> "气缸往下失败"
        0x19 -> "气缸往内失败"
        0x1A -> "气缸往外失败"
        0x1B -> "取货失败"
        else -> "货架控制板未知错误:$ec"
    }
}

private fun parseCargo2(ec: Int): String {
    return parseCargo1(ec)
}

private fun parseError(src: Int, ec: Int): String {
    return when (ec) {
        1 -> "OTA Flash 错误"
        2 -> "OTA Id 错误"
        3 -> "OTA Md5 错误"
        4 -> "OTA Size 错误"
        5 -> "无效的CMD"
        6 -> "解析参数错误"
        7 -> "无效的参数"
        else -> {
            return when (src) {
                Address.DROP -> parseDrop(ec)
                Address.DOOR -> parseDoor(ec)
                Address.HEAT -> parseHeat(ec)
                Address.CARGO1 -> parseCargo1(ec)
                Address.CARGO2 -> parseCargo2(ec)
                else -> "未知设备错误: $src => $ec"
            }
        }
    }
}













